﻿using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace myWindowsMediaPlayer
{
    public class LibraryModel : ICommand
    {
        public LibraryModel(MediaElementViewModel mediaElementViewModel)
        {
            // TODO: Complete member initialization
            this.mediaElementViewModel = mediaElementViewModel;
            mediaElementViewModel.library = new ObservableCollection<PlaylistItem>();
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        private MediaElementViewModel mediaElementViewModel;


        public void Execute(object parameter)
        {
            string action = (string)parameter;
            if (action == "Load audio")
            {
                try
                {
                    AudioPlaylist audioPlaylist = new AudioPlaylist(Library.getFromDir(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), true));
                    mediaElementViewModel.library = audioPlaylist._playlist;
                }
                catch (Exception e)
                {
                    e.ToString();
                }
            }
            else if (action == "Load video")
            {
                try
                {
                    VideoPlaylist videoPlaylist = new VideoPlaylist(Library.getFromDir(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), false));
                    mediaElementViewModel.library = videoPlaylist._playlist;
                }
                catch (Exception e)
                {
                    e.ToString();
                }
            }
        }
    }
}
